#ifndef __M_BROKER_H__
#define __N_BROKER_H__
#include "muduo/proto/codec.h"
#include "muduo/proto/dispatcher.h"
#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/TcpServer.h"
#include "../mqcommon/mq_threadpool.hpp"
#include "../mqcommon/mq_msg.pb.h"
#include "../mqcommon/mq_proto.pb.h"
#include "../mqcommon/mq_logger.hpp"
#include "mq_connection.hpp"
#include "mq_consumer.hpp"
#include "mq_host.hpp"
namespace bitmq
{
#define DBFILE "/meta.db"
#define HOSTNAME "MyVirtualHost"
    class Server
    {
    public:
        typedef std::shared_ptr<google::protobuf::Message> MessagePtr;
        Server(int port, const std::string &basedir) : _server(&_event, muduo::net::InetAddress("0.0.0.0", port),
                                                               "Server", muduo::net::TcpServer::kReusePort),
                                                       _dispatcher(std::bind(&Server::onUnknownMessage, this, std::placeholders::_1,
                                                                             std::placeholders::_2, std::placeholders::_3)),
                                                       _code(std::make_shared<ProtobufCodec>(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher,
                                                                                                        std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))),
                                                       _virtual_host(std::make_shared<VirtualHost>(HOSTNAME, basedir, basedir + DBFILE)),
                                                       _consumer_manager(std::make_shared<ConsumerManage>()),
                                                       _connection_manager(std::make_shared<ConnectionManager>()),
                                                       _threadpool(std::make_shared<Threadpool>())
        {
            // 针对历史消息中的所有队列，别忘了，初始化队列的消费者管理结构
            QueueMap qm = _virtual_host->allQueues();
            for (auto &q : qm)
            {
                _consumer_manager->initqueueconsumer(q.first);
            }
            // 注册业务需求回调处理函数
            _dispatcher.registerMessageCallback<bitmq::openChannelRequest>(std::bind(&Server::onOpenChannel, this,
                                                                                     std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
            _dispatcher.registerMessageCallback<bitmq::closeChannelRequest>(std::bind(&Server::onCloseChannel, this,
                                                                                      std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
            _dispatcher.registerMessageCallback<bitmq::declareExchangeRequest>(std::bind(&Server::onDeclareExchange, this,
                                                                                         std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
            _dispatcher.registerMessageCallback<bitmq::deleteExchangeRequest>(std::bind(&Server::onDeleteExchange, this,
                                                                                        std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
            _dispatcher.registerMessageCallback<bitmq::declareQueueRequest>(std::bind(&Server::onDeclareQueue, this,
                                                                                      std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
            _dispatcher.registerMessageCallback<bitmq::deleteQueueRequest>(std::bind(&Server::onDeleteQueue, this,
                                                                                     std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
            _dispatcher.registerMessageCallback<bitmq::queueBindRequest>(std::bind(&Server::onQueueBind, this,
                                                                                   std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
            _dispatcher.registerMessageCallback<bitmq::queueUnBindRequest>(std::bind(&Server::onQueueUnBind, this,
                                                                                     std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
            _dispatcher.registerMessageCallback<bitmq::basicPublishRequest>(std::bind(&Server::onBasicPublish, this,
                                                                                      std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
            _dispatcher.registerMessageCallback<bitmq::basicAckRequest>(std::bind(&Server::onBasicAck, this,
                                                                                  std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
            _dispatcher.registerMessageCallback<bitmq::basicConsumeRequest>(std::bind(&Server::onBasicConsume, this,
                                                                                      std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
            _dispatcher.registerMessageCallback<bitmq::basicCancelRequest>(std::bind(&Server::onBasicCancel, this,
                                                                                     std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
            // 将protobuf协议处理作为muduo信息回调函数
            _server.setMessageCallback(std::bind(&ProtobufCodec::onMessage, _code.get(), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
            _server.setConnectionCallback(std::bind(&Server::onConnection, this, std::placeholders::_1));
        }
        void start()
        {
            _server.start();
            _event.loop();
        }
    private:
        // 打开信道
        void onOpenChannel(const muduo::net::TcpConnectionPtr &conn, const openChannelRequestPtr &req, muduo::Timestamp)
        {
            Connection::ptr mconn = _connection_manager->getConnection(conn);
            if (mconn.get() == nullptr)
            {
                DLOG("打开信道时，没有找到连接对应的Connection对象！");
                conn->shutdown();
                return;
            }
            return mconn->openChannel(req);
        }
        // 关闭信道
        void onCloseChannel(const muduo::net::TcpConnectionPtr &conn, const closeChannelRequestPtr &req, muduo::Timestamp)
        {
            Connection::ptr mconn = _connection_manager->getConnection(conn);
            if (mconn.get() == nullptr)
            {
                DLOG("关闭信道时，没有找到连接对应的Connection对象！");
                conn->shutdown();
                return;
            }
            return mconn->closeChannel(req);
        }
        // 声明交换机
        void onDeclareExchange(const muduo::net::TcpConnectionPtr &conn, const declareExchangeRequestPtr &req, muduo::Timestamp)
        {
            Connection::ptr mconn = _connection_manager->getConnection(conn);
            if (mconn.get() == nullptr)
            {
                DLOG("声明交换机时，没有找到连接对应的Connection对象！");
                conn->shutdown();
                return;
            }
            Channel::ptr cp = mconn->getChannel(req->cid());
            if (cp.get() == nullptr)
            {
                DLOG("声明交换机时，没有找到信道！");
                return;
            }
            return cp->declareExchange(req);
        }
        // 删除交换机
        void onDeleteExchange(const muduo::net::TcpConnectionPtr &conn, const deleteExchangeRequestPtr &req, muduo::Timestamp)
        {
            Connection::ptr mconn = _connection_manager->getConnection(conn);
            if (mconn.get() == nullptr)
            {
                DLOG("删除交换机时，没有找到连接对应的Connection对象！");
                conn->shutdown();
                return;
            }
            Channel::ptr cp = mconn->getChannel(req->cid());
            if (cp.get() == nullptr)
            {
                DLOG("删除交换机时，没有找到信道！");
                return;
            }
            return cp->deleteExchange(req);
        }
        // 声明队列
        void onDeclareQueue(const muduo::net::TcpConnectionPtr &conn, const declareQueueRequestPtr &req, muduo::Timestamp)
        {
            Connection::ptr mconn = _connection_manager->getConnection(conn);
            if (mconn.get() == nullptr)
            {
                DLOG("声明队列时，没有找到连接对应的Connection对象！");
                conn->shutdown();
                return;
            }
            Channel::ptr cp = mconn->getChannel(req->cid());
            if (cp.get() == nullptr)
            {
                DLOG("声明队列时，没有找到信道！");
                return;
            }
            return cp->declareQueue(req);
        }
        // 删除队列
        void onDeleteQueue(const muduo::net::TcpConnectionPtr &conn, const deleteQueueRequestPtr &req, muduo::Timestamp)
        {
            Connection::ptr mconn = _connection_manager->getConnection(conn);
            if (mconn.get() == nullptr)
            {
                DLOG("删除队列时，没有找到连接对应的Connection对象！");
                conn->shutdown();
                return;
            }
            Channel::ptr cp = mconn->getChannel(req->cid());
            if (cp.get() == nullptr)
            {
                DLOG("删除队列时，没有找到信道！");
                return;
            }
            return cp->deleteQueue(req);
        }
        // 绑定队列
        void onQueueBind(const muduo::net::TcpConnectionPtr &conn, const queueBindRequestPtr &req, muduo::Timestamp)
        {
            Connection::ptr mconn = _connection_manager->getConnection(conn);
            if (mconn.get() == nullptr)
            {
                DLOG("队列绑定时，没有找到连接对应的Connection对象！");
                conn->shutdown();
                return;
            }
            Channel::ptr cp = mconn->getChannel(req->cid());
            if (cp.get() == nullptr)
            {
                DLOG("队列绑定时，没有找到信道！");
                return;
            }
            return cp->queueBind(req);
        }
        // 解除绑定
        void onQueueUnBind(const muduo::net::TcpConnectionPtr &conn, const queueUnBindRequestPtr &req, muduo::Timestamp)
        {
            Connection::ptr mconn = _connection_manager->getConnection(conn);
            if (mconn.get() == nullptr)
            {
                DLOG("队列解除绑定时，没有找到连接对应的Connection对象！");
                conn->shutdown();
                return;
            }
            Channel::ptr cp = mconn->getChannel(req->cid());
            if (cp.get() == nullptr)
            {
                DLOG("队列解除绑定时，没有找到信道！");
                return;
            }
            return cp->queueUnBind(req);
        }

        // 发布消息
        void onBasicPublish(const muduo::net::TcpConnectionPtr &conn, const basicPublishRequestPtr &req, muduo::Timestamp)
        {
            Connection::ptr mconn = _connection_manager->getConnection(conn);
            if (mconn.get() == nullptr)
            {
                DLOG("发布消息时，没有找到连接对应的Connection对象！");
                conn->shutdown();
                return;
            }
            Channel::ptr cp = mconn->getChannel(req->cid());
            if (cp.get() == nullptr)
            {
                DLOG("发布消息时，没有找到信道！");
                return;
            }
            return cp->basicPublish(req);
        }
        // 消息确认
        void onBasicAck(const muduo::net::TcpConnectionPtr &conn, const basicAckRequestPtr &req, muduo::Timestamp)
        {
            Connection::ptr mconn = _connection_manager->getConnection(conn);
            if (mconn.get() == nullptr)
            {
                DLOG("确认消息时，没有找到连接对应的Connection对象！");
                conn->shutdown();
                return;
            }
            Channel::ptr cp = mconn->getChannel(req->cid());
            if (cp.get() == nullptr)
            {
                DLOG("确认消息时，没有找到信道！");
                return;
            }
            return cp->basicAck(req);
        }
        // 订阅队列
        void onBasicConsume(const muduo::net::TcpConnectionPtr &conn, const basicConsumeRequestPtr &req, muduo::Timestamp)
        {
            Connection::ptr mconn = _connection_manager->getConnection(conn);
            if (mconn.get() == nullptr)
            {
                DLOG("队列消息订阅时，没有找到连接对应的Connection对象！");
                conn->shutdown();
                return;
            }
            Channel::ptr cp = mconn->getChannel(req->cid());
            if (cp.get() == nullptr)
            {
                DLOG("队列消息订阅时，没有找到信道！");
                return;
            }
            return cp->basicConsume(req);
        }
        // 取消订阅
        void onBasicCancel(const muduo::net::TcpConnectionPtr &conn, const basicCancelRequestPtr &req, muduo::Timestamp)
        {
            Connection::ptr mconn = _connection_manager->getConnection(conn);
            if (mconn.get() == nullptr)
            {
                DLOG("队列消息取消订阅时，没有找到连接对应的Connection对象！");
                conn->shutdown();
                return;
            }
            Channel::ptr cp = mconn->getChannel(req->cid());
            if (cp.get() == nullptr)
            {
                DLOG("队列消息取消订阅时，没有找到信道！");
                return;
            }
            return cp->basicCancel(req);
        }
        void onConnection(const muduo::net::TcpConnectionPtr &conn)
        {
            if (conn->connected())
            {
                _connection_manager->newConnection(_virtual_host, _consumer_manager, _code, conn, _threadpool);
                LOG_INFO << "新连接建立成功";
            }
            else
            {
                _connection_manager->delConnection(conn);
                LOG_INFO << "新连接关闭";
            }
        }
        void onUnknownMessage(const muduo::net::TcpConnectionPtr &conn, const MessagePtr &message, muduo::Timestamp)
        {
            LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
            conn->shutdown();
        }

    private:
        muduo::net::TcpServer _server;
        muduo::net::EventLoop _event;
        ProtobufDispatcher _dispatcher; // 事件分发器
        ProtobufCodecPtr _code;         // 协议处理器
        VirtualHost::ptr _virtual_host;
        ConsumerManage::ptr _consumer_manager;
        ConnectionManager::ptr _connection_manager;
        Threadpool::ptr _threadpool;
    };
}
#endif
